//
//  Tool.m
//  ChartsDemo
//
//  Created by wusiping on 2021/4/1.
//

#import "Tool.h"

@implementation Tool

// 是否是血氧设备
+ (BOOL)isBloodWithName:(NSString *)name{
    BOOL result = NO;
    if([name containsString:@"FS20"] || [name containsString:@"20F"] || [name containsString:@"FS20A"] || [name containsString:@"20C"] || [name containsString:@"Tv"]){
        result = YES;
    }
    return result;
}

// 是否是胎心设备
+ (BOOL)isFetalHeartWithName:(NSString *)name{
    return [name containsString:@"AD"];
}

// 是否是手表设备
+ (BOOL)isWatchWithName:(NSString *)name{
    return [name containsString:@"WS"] || [name containsString:@"WS10"];
}

// 是否是高原血氧仪设备
+ (BOOL)isPlateauWithName:(NSString *)name{
    return [name containsString:@"FS20L-"];
}
// 是否是盆底肌设备
+ (BOOL)isPelvicMusclesWithName:(NSString *)name{
    BOOL result = NO;
    if([name containsString:@"PF"] || [name containsString:@"PH12_"] || [name containsString:@"PC_"] || [name containsString:@"PF12_"]){
        result = YES;
    }
    return result;
}

// 判断设备类型
+ (ARTDeviceType)deviceTypeWithName:(NSString *)name{
//    if ([self isPlateauWithName:name]){
//        return ARTDeviceTypePlateau;
//    }
//    if ([self isBloodWithName:name]) {
//        return ARTDeviceTypeOximeter;
//    }
//    if ([self isFetalHeartWithName:name]){
//        return ARTDeviceTypeFetalHeart;
//    }
    if ([self isWatchWithName:name]){
        return ARTDeviceTypeWatch;
    }
//    if ([self isPelvicMusclesWithName:name]){
//        return ARTDeviceTypePelvicMuscles;
//    }
    return ARTDeviceTypeUnknow;
}

// 获取设备mac地址
+ (NSString *)macAddressWithAdvertisement:(NSDictionary *)advertisementData{
//    NSData * data = advertisementData[CBAdvertisementDataManufacturerDataKey];
//    int len = 6;
//    NSData * macAddressData;
//    NSString *macAddress;
//    // 获得物理地址
//    if (data.length >= len) {
//        NSRange range = NSMakeRange(data.length - len, len);
//        macAddressData = [data subdataWithRange:range];
//        macAddress = macAddressData.length ? [self formatterMacAddress:macAddressData] :@"";
//    }else{
//        macAddress = @"";
//    }
//    return macAddress;
    NSLog(@"advertisementData=%@",advertisementData);
    NSData *data = [advertisementData objectForKey:@"kCBAdvDataManufacturerData"];
    NSString *mac = @"";
    if (data.length == 16) {
        NSArray *datas = @[
            [data subdataWithRange:NSMakeRange(4, 1)],
            [data subdataWithRange:NSMakeRange(5, 1)],
            [data subdataWithRange:NSMakeRange(6, 1)],
            [data subdataWithRange:NSMakeRange(7, 1)],
            [data subdataWithRange:NSMakeRange(8, 1)],
            [data subdataWithRange:NSMakeRange(9, 1)],
        ];
        NSMutableArray *macs = [NSMutableArray array];
        for (NSData *temp in datas) {
            [macs addObject:[[ASFHConverter convert_16String_with_16Data:temp] uppercaseString]];
        }
        mac = [macs componentsJoinedByString:@":"];
    }
    return mac;
}


+ (UIViewController *)getCurrentVC {
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        result = nextResponder;
    } else {
        result = window.rootViewController;
    }
    return result;
}

+ (NSString *)getMMSSFromSS:(NSInteger)seconds{
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    
    if (seconds <= 0) {
        return format_time = @"00:00:00";
    }
    return format_time;
}

// 秒格式化 分秒
+ (NSString *)get_MMSS_FromSS:(NSInteger)seconds{
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",seconds/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
    
    if (seconds <= 0) {
        return format_time = @"00:00";
    }
    return format_time;
}

/**
 * 将时间戳转化为字符串类型的时间
 *
 * @param timestamp 时间戳
 * @param format 转化为字符串后的格式 例如：yyyy-MM-dd HH:mm:ss
 * @return 转化后的字符串
 */
+ (NSString *)art_timestampSwitchTime:(long)timestamp
                         andFormatter:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
//    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    [formatter setTimeZone:timeZone];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}

/// 播报语音
+ (void)startingVoiceBroadcastWithAudioName:(NSString *)audioName{
//    BOOL canPlay = [NSUserDefaults boolForKey:ART_Pelvic_Floor_Muscles_VioceStatus];
//    if (!canPlay) {
//        if (audioName.length > 0) {
//            NSString *language = [FYLocalizeableMode currentLanguage];
//            if ([language isEqualToString:@"0"]) {
//                // ch
//            }else if ([language isEqualToString:@"4"]){
//                // es
//                audioName = [NSString stringWithFormat:@"%@_es",audioName];
//            }else {
//                // en
//                audioName = [NSString stringWithFormat:@"%@_en",audioName];
//            }
//            NSString *audioPath = [[NSBundle mainBundle] pathForResource:audioName ofType:@".mp3"];
//            ARTAvdioPlayer *player = [ARTAvdioPlayer shareInstance];
//            [player changeAudioSessionCategory:AVAudioSessionCategoryPlayback];
//            [player playAvdioWithUrl:audioPath];
//        }
//    }
}

/// 计算年龄
/// @param startTime <#startTime description#>
+ (NSInteger)calculateAgeWithSartDate:(NSString *)startTime{
    if (startTime.length == 0){
        return 0;;
    }
    NSDate *date=[NSDate date];
    //获取当前时间
    NSTimeZone *timeZone=[NSTimeZone systemTimeZone];//返回一个系统的时区
    NSInteger interval=[timeZone secondsFromGMTForDate:date];
    NSDate * localdate=[date dateByAddingTimeInterval:interval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    NSString *endDateStr = [dateFormatter stringFromDate:localdate];
    NSDate *startDate = [dateFormatter dateFromString:startTime];
    NSDate *endDate = [dateFormatter dateFromString:endDateStr];
    // NSCalendar: 比较日期的差异
    NSCalendar *calendar = [NSCalendar currentCalendar];
    /**
     * 要比较的时间单位,常用如下,可以同时传：
     *    NSCalendarUnitDay : 天
     *    NSCalendarUnitYear : 年
     *    NSCalendarUnitMonth : 月
     *    NSCalendarUnitHour : 时
     *    NSCalendarUnitMinute : 分
     *    NSCalendarUnitSecond : 秒
     */
    NSCalendarUnit unit = NSCalendarUnitYear;//只比较天数差异
    //比较的结果是NSDateComponents类对象
    NSDateComponents *delta = [calendar components:unit fromDate:startDate toDate:endDate options:0];
    NSInteger count = delta.year;
    return count >0?count:1;
}

/// 回到实时页面
+ (void)skipToTabBarController{
    if (ARTSingleShare.hadLoadTabVC) {
        NSMutableArray *vcArray = [NSMutableArray array];
        for (UIViewController *vc in [ARTNavigation currentViewController].navigationController.viewControllers) {
            NSString *clsName = NSStringFromClass([vc class]);
            if ([clsName isEqualToString:@"ARTScanDeviceController"] || [clsName isEqualToString:@"ARTMainTabController"] ) {
                [vcArray addObject:vc];
            }
        }
        [[ARTNavigation currentViewController].navigationController setViewControllers:vcArray];
    }
}

/// 获取盆底肌所有病症
+ (NSArray <NSString *> *)getPelvicMusclesTotalSymptoms{
//    return @[@"压力性尿失禁",@"混合型尿失禁",@"急迫性尿失禁",@"人流术后疼痛",@"慢性盆底疼痛",@"阴道松弛",@"性功能障碍",@"阴道口闭合",@"子宫内膜薄",@"脏器脱垂",@"盆底肌评估",@"压力性尿失禁",@"混合型尿失禁",@"急迫性尿失禁",@"人流术后疼痛",@"慢性盆底疼痛",@"阴道松弛",@"性功能障碍",@"阴道口闭合",@"子宫内膜薄",@"脏器脱垂",@"产后腰背痛",@"肩颈疼痛",@"产后恢复",@"产后排尿",@"腹直肌康复",@"产后催乳",@"乳腺疏通",@"乳腺增生",@"乳房保养"];
    return @[FYLocalizedString(@"pelvic_floor_treatment_type1", @""),FYLocalizedString(@"pelvic_floor_treatment_type2", @""),FYLocalizedString(@"pelvic_floor_treatment_type4", @""),FYLocalizedString(@"pelvic_floor_treatment_type5", @""),FYLocalizedString(@"pelvic_floor_treatment_type3", @""),FYLocalizedString(@"pelvic_floor_treatment_type6", @""),FYLocalizedString(@"pelvic_floor_treatment_type7", @""),FYLocalizedString(@"pelvic_floor_treatment_type8", @""),FYLocalizedString(@"pelvic_floor_treatment_type9", @""),FYLocalizedString(@"pelvic_floor_treatment_type10", @""),@"盆底肌评估",FYLocalizedString(@"pelvic_floor_treatment_type1", @""),FYLocalizedString(@"pelvic_floor_treatment_type2", @""),FYLocalizedString(@"pelvic_floor_treatment_type4", @""),FYLocalizedString(@"pelvic_floor_treatment_type5", @""),FYLocalizedString(@"pelvic_floor_treatment_type3", @""),FYLocalizedString(@"pelvic_floor_treatment_type6", @""),FYLocalizedString(@"pelvic_floor_treatment_type7", @""),FYLocalizedString(@"pelvic_floor_treatment_type8", @""),FYLocalizedString(@"pelvic_floor_treatment_type9", @""),FYLocalizedString(@"pelvic_floor_treatment_type10", @""),FYLocalizedString(@"pelvic_floor_treatment_type15", @""),FYLocalizedString(@"pelvic_floor_treatment_type19", @""),FYLocalizedString(@"pelvic_floor_treatment_type16", @""),FYLocalizedString(@"pelvic_floor_treatment_type17", @""),FYLocalizedString(@"pelvic_floor_treatment_type18", @""),FYLocalizedString(@"pelvic_floor_treatment_type12", @""),FYLocalizedString(@"pelvic_floor_treatment_type11", @""),FYLocalizedString(@"pelvic_floor_treatment_type13", @""),FYLocalizedString(@"pelvic_floor_treatment_type14", @"")];
}

// 秒转化成分秒 --字符串
+ (NSString *)getNormalMMSSWithSS:(NSInteger)seconds{
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%ld",seconds/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%ld",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@%@%@%@",str_minute,FYLocalizedString(@"minute_", @""),str_second,FYLocalizedString(@"second_", @"")];
    
    if (seconds <= 0) {
        return format_time = @"0分:0秒";
    }
    return format_time;
}

@end
